package test;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map.Entry;
import java.util.Set;

import javax.sql.DataSource;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.baemusic.dao.AreaDao;
import com.baemusic.domain.Area;
import com.baoyi.apps.Weatherinfo;
import com.baoyi.apps.WeatherinfoApps;
import com.baoyi.city.City;
import com.baoyi.city.Provshi;
import com.baoyi.city.Station;
import com.baoyi.http.Connection;
import com.baoyi.http.HttpConnection;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

public class Apps {

	/**
	 * @param args
	 * @throws IOException
	 */
	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
		context.scan("com.baemusic");
		context.refresh();
		ApplicationContext c = new ClassPathXmlApplicationContext(
				"applicationContext.xml");
		DataSource d = c.getBean(DataSource.class);
		AreaDao a = context.getBean(AreaDao.class);
		a.setDataSource(d);
		Area as = a.findByName("重庆");
		System.out.println(as);
		String code=as.getCode();
		if(code.indexOf("00")>0){
			code=code.replace("00", "");
			code=code+"00";
			//101041300
			//101041000
			//101041000
			System.out.println(code);
			Weatherinfo info = WeatherinfoApps.FindByCode(code);
			System.out.println(info);
		}else{
			Weatherinfo info = WeatherinfoApps.FindByCode(as.getCode());
			System.out.println(info);
		}
		
	}

	private static void dd(AreaDao a) throws IOException {

		List<Provshi> ps = china();
		for (Provshi provshi : ps) {

			Area item = new Area();
			item.setCode(provshi.getCode());
			item.setName(provshi.getName());
			item.setLevels(1);
			item.setPcode("-1");
			a.add(item);
			System.out.println(item.getName());
			List<City> cs = china(provshi.getCode());
			for (City city : cs) {
				item = new Area();
				item.setLevels(2);
				item.setCode(provshi.getCode() + city.getCode());
				item.setName(city.getName());
				item.setPcode(provshi.getCode());
				a.add(item);
				System.out.println("******:" + item);
				List<Station> ss = findStationByCode(item.getCode());
				for (Station station : ss) {
					item = new Area();
					item.setLevels(3);
					item.setCode(provshi.getCode() + city.getCode()
							+ station.getCode());
					item.setName(station.getName());
					item.setPcode(provshi.getCode() + city.getCode());
					a.add(item);
					System.out.println("########:" + item);
				}
			}
		}
	}

	public static List<Provshi> china() throws IOException {
		Connection connection = HttpConnection.connect(app);
		connection.header("Referer", "http://www.weather.com.cn/");
		connection
				.header("User-Agent",
						"Mozilla/5.0 (Windows NT 5.1; rv:14.0) Gecko/20100101 Firefox/14.0.1");
		String msg = connection.execute().body();
		JsonParser parser = new JsonParser();
		List<Provshi> datas = new ArrayList<Provshi>();
		JsonElement jsons = parser.parse(msg);
		JsonObject ob = jsons.getAsJsonObject();
		Set<Entry<String, JsonElement>> ss = ob.entrySet();
		for (Entry<String, JsonElement> entry : ss) {
			Provshi item = new Provshi();
			item.setCode(entry.getKey());
			item.setName(entry.getValue().getAsString());
			datas.add(item);

		}
		return datas;
	}

	static String app = "http://www.weather.com.cn/data/city3jdata/china.html";

	public static List<City> china(String code) throws IOException {
		Connection connection = HttpConnection
				.connect("http://www.weather.com.cn/data/city3jdata/provshi/"
						+ code + ".html");
		connection.header("Referer", "http://www.weather.com.cn/");
		connection
				.header("User-Agent",
						"Mozilla/5.0 (Windows NT 5.1; rv:14.0) Gecko/20100101 Firefox/14.0.1");
		String msg = connection.execute().body();
		JsonParser parser = new JsonParser();
		List<City> datas = new ArrayList<City>();
		JsonElement jsons = parser.parse(msg);
		JsonObject ob = jsons.getAsJsonObject();
		Set<Entry<String, JsonElement>> ss = ob.entrySet();
		for (Entry<String, JsonElement> entry : ss) {
			City item = new City();
			item.setCode(entry.getKey());
			item.setName(entry.getValue().getAsString());
			datas.add(item);

		}
		return datas;
	}

	public static List<Station> findStationByCode(String code)
			throws IOException {
		Connection connection = HttpConnection
				.connect("http://www.weather.com.cn/data/city3jdata/station/"
						+ code + ".html");
		connection.header("Referer", "http://www.weather.com.cn/");
		connection
				.header("User-Agent",
						"Mozilla/5.0 (Windows NT 5.1; rv:14.0) Gecko/20100101 Firefox/14.0.1");
		String msg = connection.execute().body();
		JsonParser parser = new JsonParser();
		List<Station> datas = new ArrayList<Station>();
		JsonElement jsons = parser.parse(msg);
		JsonObject ob = jsons.getAsJsonObject();
		Set<Entry<String, JsonElement>> ss = ob.entrySet();
		for (Entry<String, JsonElement> entry : ss) {
			Station item = new Station();
			item.setCode(entry.getKey());
			item.setName(entry.getValue().getAsString());
			datas.add(item);

		}
		return datas;
	}
}
